<?php
header("Access-Control-Allow-Origin: *");
//header("Access-Control-Allow-Origin: http://wlscjia.com");
//header("Access-Control-Allow-Methods: GET");
//header("Access-Control-Allow-Methods: POST");
header("Allow-Control-Allow-Origin: *");
header('Content-Type:text/html;charset=utf8');
define('APP_PATH', __DIR__ . '/../application/');
ini_set('memory_limit','3072M');
set_time_limit(120);
if ($_GET[s_url] && $_GET[gather_type]) {
    $ds_url = urlencode($_GET[s_url]);
    $url="http://collect1.longsunhd.com/collect/index.php?bug=0&g={$_GET['g']}&gather_type={$_GET['gather_type']}&s_url=$ds_url";
    //$r=file_get_contents($url);
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 0);
    $r = curl_exec($ch);
    curl_getinfo( $ch, CURLINFO_HEADER_OUT);
    curl_close($ch);
    echo $r;
    exit;
}


?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Language" content="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Access-Control-Allow-Origin" content="http://wlscjia.com">
    <title>秀美图</title>
    <link rel="stylesheet" type="text/css" href="/pbl/reset.css"/>
    <script type="text/javascript" src="/pbl/static/js/jquery-1.7.1.min.js"></script>
    <link type="text/css" rel="stylesheet" href="/pbl/waterfall/css/waterfall.css"/>
    <link type="text/css" rel="stylesheet" href="/pbl/waterfall/css/font-awesome.min.css"/>
    <script type="text/javascript" language="javascript" src="/pbl/waterfall/js/waterfall.js"></script>
    <script type="text/javascript" language="javascript" src="/pbl/static/js/layer/layer.js"></script>
    <script type="text/javascript" language="javascript" src="/pbl/static/js/ZeroClipboard.min.js"></script>

    <script>
        var windex;
        var viid = 0;
        var url;
        var listmax=0;
        var ajaxGet;

        function testt(bug, pbl,wztype) {
            var wztype=(wztype=="")?1:wztype;
            url=o.url.value;
            if (o.url.value.match(/\[([0-9]+)-([0-9]+)\]/)) {
                page = RegExp.$1;
                url = o.url.value.replace(/\[([0-9]+)-([0-9]+)\]/, RegExp.$1);
                doatest(url, bug, '<?php echo $_GET[g]?>', "html", pbl,listmax);
            } else {
                doatest(o.url.value, bug, '<?php echo $_GET[g]?>', "html", pbl,listmax);
            }

        }


        function changeUrlWithPage() {
            if (!/\[([0-9]+)-([0-9]+)\]/.test(o.url.value)) return;
            o.url.value.match(/\[([0-9]+)-([0-9]+)\]/);
            if (parseInt(RegExp.$1) < parseInt(RegExp.$2)) {
                url = o.url.value.replace(/\[([0-9]+)-([0-9]+)\]/, page++);
                if (page >= RegExp.$2) return;
            } else {
                url = o.url.value.replace(/\[([0-9]+)-([0-9]+)\]/, page--);
                if (page <= RegExp.$2) return;
            }
            setTimeout(function () {
                layer.msg('正在加载' + url + "...");
            }, 10000);
            doatest(url, 0, '<?php echo $_GET[g]?>', "html", 1,0);
        }


        function doatest(url, ao, google, dataType, pbl,fromcacheid) {
            windex = layer.load(0, {shade: false});
            $("#B2").attr("disabled", "true");
            $("#B1").attr("disabled", "true");
            if(url==""){
                 url="http://<?php echo $_SERVER['HTTP_HOST']?>/pbl/getparsetimgs.php";
                $.post(url, {"html":$("#zztt")[0].contentWindow.document.body.innerHTML,"uid":1}, function(data,textStatus){
                    layer.msg("代码保存完成！请重新抓取！");
                    layer.close(windex);
                    $("#zztt")[0].contentWindow.document.body.innerHTML="";
                    $("#s_url").val(url);
                    $("select[name='gather_type']").val(1);
                    $("#B2").removeAttr("disabled");
                    $("#B1").removeAttr("disabled");
                    return;
                });
                layer.close(windex);
                $("s_url").val(url);
                return ;
            }
            var action = "/collect/index.php";
            ajaxGet =$.ajax({
                type: 'get',
                async: true,
                timeout: 20000,
                url: action,//?XDEBUG_SESSION_START=080808
                data: {
                    //onlyread: 1,
                    gather_type: o.gather_type.value,
                    s_url: url,
                    bug: 0,
                    g: google,
                    XDEBUG_SESSION_START: 8888,
                    fromcacheid:fromcacheid,
                    s: Math.random()
                },
                dataType: dataType,
                success: function (data) {
                    layer.close(windex);
                    $("#B2").removeAttr("disabled");
                    $("#B1").removeAttr("disabled");
                    data = eval('(' + data + ')');
                    if (pbl) {
                        if(data.subject=="抓取页存在多个列表，请按提示重新输入要抓取的网址"){
                            layer.open({
                                type: 1,
                                skin: 'layui-layer-rim', //加上边框
                                area: ['800px', '600px'], //宽高
                                content: data.message,
                            });
                        }else {
                            var json = new Array();
                            var patt = new RegExp("\\[img\\]([^<>]+?)\\[\\/img\\](?:[^<>\\[\\]]*\\[(?:fromurl|formurl)\\]([^<>]+?)\\[\\/(?:fromurl|formurl)\\])?", "gi");
                            var result;
                            var index = 0;
                            if (data.listmax == 0) {
                                layer.msg(url + ' 已经加载完成!');
                            }
                            listmax = data.listmax;
                            while ((result = patt.exec(data.message)) != null) {
                                json[index] = new Array();
                                json[index]['image'] = result[1];
                                json[index]['subject'] = gettitle(result[1], data.message, data.subject);
                                data.url = (data.url == "") ? url : data.url;
                                json[index]['url'] = (result[2] != "" && typeof(result[2]) != "undefined") ? result[2] : data.url;
                                json[index]['fromurl'] = json[index]['url'];
                                index++;
                            }
                            if(ajaxGet != null){
                                loadMore(json, changeUrlWithPage());
                            }
                        }
                    }else{
                        $("#content").show();
                        data.message = data.message.replace(/\r?\n/gi, "<p>");
                        data.message = data.message.replace(/\[align=center\]/gi, "<p align=center>");
                        data.message = data.message.replace(/\[align=left\]/gi, "<p align=left>");
                        data.message = data.message.replace(/\[\/align\]/gi, "</p>");
                        data.message = data.message.replace(/\[img\](.*?)\[\/img\]/gi, "<p align=center><img src=\"$1\"/></p>");
                        data.message = data.message.replace(/\[url=([^<>\[\]]+)\](.*?)\[\/url\]/gi, "<a target=_blank href=\"$1\">$2</a>");
                        data.message = data.message.replace(/\[img\]/gi, "<img src=\"");
                        data.message = data.message.replace(/\[\/img\]/gi, "\">");
                        data.message = data.message.replace(/\[b\]([^\[\]]*)\[\/b\]/gi, "<b>$1</b>");
                        data.message = data.message.replace(/\[b\]|\[\/b\]/gi, "");
                        data.message = data.message.replace(/\[flash[0-9\,\s\=]*\](.*?)\[\/flash\]/gi, function ($0, $1) {
                            viid++;
                            return "<div class=\"video\" id=\"CuPlayer" + viid.toString() + "\" style=\"950px;margin:0 auto;\"></div>\r\n	<scri" + "pt type=\"text/javascript\">\r\nvar so = new SWFObject(\"/PlayerLite/PlayerLite.swf\",\"CuPlayerV4\",\"520\",\"325\",\"9\",\"#000000\");\r\nso.addParam(\"allowfullscreen\",\"true\");\r\nso.addParam(\"allowscriptaccess\",\"always\");\r\nso.addParam(\"wmode\",\"opaque\");\r\nso.addParam(\"quality\",\"high\");\r\nso.addParam(\"salign\",\"lt\");\r\nso.addVariable(\"videoDefault\",\"" + $1 + "\"); //\u89C6\u9891\u6587\u4EF6\u5730\u5740\r\nso.addVariable(\"autoHide\",\"true\");\r\nso.addVariable(\"hideType\",\"fade\");\r\nso.addVariable(\"autoStart\",\"false\");\r\nso.addVariable(\"holdImage\",\"/PlayerLite/start.jpg\");\r\nso.addVariable(\"startVol\",\"60\");\r\nso.addVariable(\"hideDelay\",\"60\");\r\nso.addVariable(\"bgAlpha\",\"75\");\r\nso.write(\"CuPlayer" + viid.toString() + "\");\r\n</scr" + "ipt>";
                        });
                        $("#subject").html(data.subject);
                        $("#message").html(data.message.replace(/</gi,"&lt;").replace(/>/gi,"&gt;"));
                        //document.body.getElementsById("message").innerHTML=data.message;
                        //alert(data.message);
                        $("#dateline").html(data.dateline);
                        if(data.laiyuan>"") $("#laiyuan").html("来源:"+data.laiyuan);
                        if(data.laiyuan!=data.plate&&data.plate!="") $("#plate").html("栏目:"+data.plate);
                        if(data.author!="") $("#author").html("作者:"+data.author);
                        if(data.views>0) $("#views").html("浏览数:"+data.views);
                        if(data.replies>0) $("#replies").html("回复数:"+data.replies);

                        //自定页
                        layer.open({
                            type: 1,
                            skin: 'layui-layer-rim', //样式类名
                            closeBtn: 1,
                            area: ['90%', '90%'],
                            shade: 0,
                            shadeClose: false,
                            content: $("#content").html().replace(/&lt;/gi,"<").replace(/&gt;/gi,">"),
                            success: function(){
                                $(".layui-layer-title").html("龙抓手采集测试：<b>"+data.subject+"</b>");
//                                debugger;
//                                ZeroClipboard.config( { swfPath: 'http://bbs.longsunhd.com/static/js/ZeroClipboard.swf' } );
//                                var clip1 = new ZeroClipboard(document.getElementById("clip_button1"));
//                                clip1.clip("gfdsgdfsere");
                            }
                        });


                    }
                },

                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(XMLHttpRequest.status);
                    //alert(XMLHttpRequest.readyState);
                    //alert(textStatus);
                    //layer.msg("超时了!\r\n<br>请检查提交的网址并选择对网址类型！");
                    layer.close(windex);
                    if(listmax==0){
                        listmax=1;
                        if(ajaxGet != null) {
                            doatest(url, 0, '<?php echo $_GET[g]?>', "html", 1, listmax);
                        }
                    }
                    $("#B2").removeAttr("disabled");
                    $("#B1").removeAttr("disabled");
                },

//                complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
//                    if (status == 'timeout') {//超时,status还有success,error等值的情况
//                        ajaxTimeoutTest.abort();
//                        $("#B2").removeAttr("disabled");
//                        $("#B1").removeAttr("disabled");
//                        alert("超时");
//                    }
//                }
                //,cache:false, // disable cache(禁用IE缓存)
                //,ifModified:true
            });

            return;
        }

        function count(o) {
            var t = typeof o;
            if (t == 'string') {
                return o.length;
            } else if (t == 'object') {
                var n = 0;
                for (var i in o) {
                    n++;
                }
                return n;
            }
            return false;
        }

        function fz(o,wztype) {
            wztype=(typeof(wztype)=="undefined")?6:wztype;
            $("input[name=url]").val(o.innerHTML);
            $("select[name='gather_type']").val(wztype);
            testt(0,1,wztype);
        }

        function openimg(url, o) {
            $("div.topright").remove();
            $(o).parents("div").height($(o).parents("div").height());
            if ($(o).hasClass("imgon")) {
                $(".imgon").removeClass("imgon").removeAttr("style");
                return;
            }
            $(".imgon").removeClass("imgon").removeAttr("style");
            $(o).addClass("imgon");
            var width = o.offsetWidth;
            var height = o.offsetHeight;
            $(o).attr("style", "margin-left:-" + (width / 2) + "px;top:" + ($(document).scrollTop() + 30) + "px");
            $(o).closest("div").append("<div class='topright' style='width:"+width+"px;margin-left:-" + (width / 2) + "px;top:" + ($(document).scrollTop() + 50) + "px'><span class='sc fa fa-heart fa-2x fa-fw'></span></div>");
        }

        function gettitle(url, nr, bt) {
            var re = new RegExp("==[\\r\\n\\s]*((?:[^=\\r\\n](?!==))*)[\\r\\n](?:[\\S\\s](?!==))*" + addsla(url), "gi");
            if ((result = re.exec(nr)) != null) {
                return result[1].replace(/^\s/, '');
            } else {
                return bt;
            }
        }

        function addsla(str) {
            str = str.replace(/([\.\/\?\:\&])/g, "\\$1");
            return str;
        }

        function clw() {
            $(".imgon").removeClass("imgon");
        }


        function stopall() {
            if(ajaxGet!=null) ajaxGet.abort();
            if (!!(window.attachEvent && !window.opera)) {
                document.execCommand("stop");
            } else {
                window.stop();
            }
            return false;
        }

        function qingkong(){
            //$("#s_url").val("");
            $("#zztt")[0].contentWindow.document.body.innerHTML="";
            layer.closeAll();
            listmax=0;
            $("#stage>li").html("");
            stopall();
        }


        //打开我的收藏列表
        function shouc(){
            layer.open({
                type: 2,
                title: '我的抓取更多列表',
                shadeClose: true,
                shade: 0.2,
                area: ['800px', '70%'],
                content: 'http://www.wlscjia.com/admin/longzs/lists/listindex?host=<?php echo $_SERVER['HTTP_HOST']?>'
            });
        }


        //插入一条列表数据
        function getlisturl(){
            var index = layer.load(0, {shade: false});
            $.ajax({
                type:'get',
                async:true,//true是异步，false是同步
                timeout : 20000,//20秒
                url: 'http://www.wlscjia.com/admin/longzs/lists/insert', //"?rn="+Math.random(),  //网址请以/开头否则可能不带cookies过去
                data: {
                    "row[listurl]": encodeURIComponent($("#s_url").val()),
                    "row[title]": encodeURIComponent("新列表"),
                    "row[type_list]": encodeURIComponent("列表页"),
                    "row[uid]": 1,
                    "row[verify_switch]": 1,
                }, //data里面的值不要有未定认或null值
                dataType: "jsonp",//html,text,json,jsonp,excel,script
                success: function(obj){
                    if(obj.code==0){
                        $("#scurl").removeClass("red")
                    }else{
                        $("#scurl").addClass("red")
                    }
                    layer.close(index)
                },
                //error: function(XMLHttpRequest, textStatus, errorThrown) {
                //    alert("出错:"+XMLHttpRequest.status);
                //     //alert(XMLHttpRequest.readyState);
                //     //alert(textStatus);
                // },
                //complete : function(XMLHttpRequest,status){
                //     //请求完成后最终执行参数
                //     alert("完成:"+status);
                //     if(status=='timeout'){
                //     //超时,status还有success,error等值的情况
                //     ajaxTimeoutTest.abort();
                //    }
                // }
                //,cache:false, // disable cache(禁用IE缓存)
                //,ifModified:true
            });
        }

       function doedit(){
            $("#zztt")[0].contentWindow.document.body.contentEditable= "true";
        }
    </script>
</head>
<body style="margin:0;text-align:center;">
<table border="0" width="100%" height=100% style="margin-left:-3px">
    <tr>
        <td id="toptd" style="height:35px">
            <div id="fix">
                <div class="menu-bar wrapper" style="width: 1496px;">
                    <div class="left-part">
                        <div>
                        <a id="tuyuan" href="/"></a>
                        <a href="/" data-title="home" class="header-item active">首页</a>
                        <form method="POST" action="/collect/index.php" class="searching-unit" name=o style="display:inline-block">
                            <div style="float:left;margin-top:-1px;margin-right:5px">
                            <input type="text" style="width:500px" id="s_url" onmouseover="this.focus();this.select();"
                                   onkeypress="if (event.keyCode == 13||event.keyCode == 9){testt(0);return false;}"
                                   placeholder="搜索关键词、网址" name="url" value="<?php
                            $_GET[url]=($_GET[url])?$_GET[url]:"http://365ye.cn/3/page_2.html?zq=2";
                            echo $_GET[url] ?>">
                            <span id="scurl" class='fa fa-heart fa-1x fa-fw' style=" position: absolute; margin-left: -32px;margin-top: 9px;" onclick=getlisturl()></span>
                            <select name="gather_type" style="display:">
                                <option value=1>从正文页</option>
                                <option value=6 selected>从列表页</option>
                                <option value=7>从图片地址</option>
                            </select>

                            <input type="button" value="采集图片" name="B2" id="B2" onclick=testt(0,1)>
                            <input type="button" value="采集文章" name="B1" id="B1" onclick=testt(0,0)>
                            <input type="button" value="我的采集" name="B3" id="B3" onclick=shouc()>
                            <input type="button" value="清空" name="B4" id="B4" onclick=qingkong()>
                            <input type="button" value="停止" name="B5" id="B5" onclick=stopall()>
                            <iframe id="zztt" src="kongbai.html" scrolling=no onload="doedit()" onclick="javascript:this.focus()"></iframe>
                            <!--a target="_blank" href="http://collect.longsunhd.com/getcss">部分样式提取工具</a-->
                        </form>
                        </div>
                    </div>

                </div>


                <iframe id="ifr" name="ifr" width=0 height=0 style="width:0px;height:0px;display:none"></iframe>
            </div>
        </td>
    </tr>
</table>
<div id="content">
<h1><b><span id="subject1"></span></b></h1>
<h1><b><span id="subject"></span></b></h1>
<h1><b><span id="subject2"></span></b></h1>
    <span id="dateline"></span>&nbsp;<span id="laiyuan"></span>&nbsp;<span id="plate"></span>&nbsp;<span id="author"></span>&nbsp;<span id="views"></span>&nbsp;<span id="replies"></span>&nbsp;
    【
    <span class="clip_container" id="clip_container1"><span id="clip_button1" style="width:50px;height:24px"><b>复制</b></span></span>
     】
    <div id="message">

    </div>
</div>
<ul id="stage">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

</html>